<?php
namespace app\admin\controller;

use app\admin\model\Attachment;
use think\Controller;
use think\Loader;


class Page extends Admin
{
	/**
	 * 规则列表
	 *
	 * @author chengbin
	 */
	public function index()
	{
        return $this->fetch();
	}

    /**
     * @return View
     */
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
//        exit(var_dump($file->getExtension()));
        if(!$file){
            return ['status'=>false,'msg'=>'请选择图片'];
        }
        if(!is_dir(ROOT_PATH . 'public' . DS . 'uploads')){
            mkdir(ROOT_PATH . 'public' . DS . 'uploads');
        }
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            return ['status'=>true,'msg'=>'','src'=>DS.'uploads'.DS.$info->getSaveName()];
        }else{
            // 上传失败获取错误信息
            return ['status'=>false,'msg'=>$file->getError()];
        }
    }

	/**
	 * 异步获取列表数据
	 *
	 * @author chengbin
	 * @return mixed
	 */
	public function getData()
	{
		if(!request()->isAjax()) {
			$this->error(lang('Request type error'), 4001);
		}
		$request = input('get.');
		$data = model('Attachment')->getList( $request );
		return $data;
	}

	/**
	 * 添加规则
	 *
	 * @author chengbin
	 */
	public function add()
	{
		return view();
	}



	/**
	 * 编辑规则
	 *
	 * @author chengbin
	 */
	public function edit( $id = '' )
	{
		$data = model('Attachment')->get(['id'=>$id]);
		$this->assign( 'data', $data );
		return view();
	}

	/**
	 * 保存数据
	 */
	public function saveData()
	{
		if(!request()->isAjax()) {
			return info(lang('Request type error'));
		}
		$data = input('post.');
		model('Attachment')->saveData($data);
		$this->success(lang('Save success'));
	}

	/**
	 * 删除
	 */
	public function delete($id = 0){
		if(empty($id)){
			return info(lang('Data ID exception'), 0);
		}
		return model('Attachment')->deleteById($id);
	}


}